//
//  ESTFollowUpRecordViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/24.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTFollowUpRecordViewController.h"
#import "ESTFollowUpRecordtHeaderView.h"
#import "ESTFollowUpRecordTableViewCell.h"
#import "ESTCustomerInfoModel.h"
#import "ESTActivityEffectiveView.h"
#import "ESTFollowUpHistoryModel.h"
#import "ESTCarBrandTableViewController.h"
#import "TYZPickerTool.h"
#import "ESTFollowUpHistoryInfoCell.h"
#import "TYZCallPhoneTool.h"
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>

@interface ESTFollowUpRecordViewController ()
/**
 *  本次录入的HeaderView的数据源
 */
@property(nonatomic, strong) NSMutableArray<ESTFollowUpRecordHeaderViewModel *> *headerViewDataArray;
/**
 *  记录HeaderView的数据源中移除的项
 */
@property(nonatomic, strong) NSMutableArray<ESTFollowUpRecordHeaderViewModel *> *headerViewUnDataArray;
/**
 *  含历史记录HeaderView的数据源
 */
@property(nonatomic, strong) NSArray<ESTFollowUpRecordHeaderViewModel *> *headerViewHistoryDataArray;
/**
 *  记录上一次的本次录入HeaderView的数据源
 */
@property(nonatomic, strong) NSArray<ESTFollowUpRecordHeaderViewModel *> *headerViewLastDataArray;
/**
 *  本次录入的Cell的数据源
 */
@property(nonatomic, strong) NSMutableArray<NSMutableArray<ESTFollowUpRecordCellModel *> *> *cellDataArray;
/**
 *  记录Cell的数据源中被移除的项
 */
@property(nonatomic, strong) NSMutableArray<NSMutableArray<ESTFollowUpRecordCellModel *> *> *cellUnDataArray;
/**
 *  历史记录Cell的数据源
 */
@property(nonatomic, strong) NSArray<ESTFollowUpHistoryInfoCell *> *cellHistoryDataArray;
/**
 *  控制组头展开收缩的数组
 */
@property(nonatomic, strong) NSMutableArray<NSString *> *sectionNumber;
/**
 *  是否选择查看历史回访
 */
@property(nonatomic, assign) BOOL selectedHistory;
/**
 *  历史信息
 */
@property(nonatomic, strong) NSArray<ESTFollowUpHistoryModel *> *historyDataArray;
/**
 *  cell的高度
 */
@property(nonatomic, strong) NSMutableArray<NSNumber *> *cellHeightArray;
/**
 *  左边标题文本数组
 */
@property(nonatomic, strong) NSArray<NSArray<NSString *> *> *titleArray;
/**
 *  子标题文本数组
 */
@property(nonatomic, strong) NSArray<NSArray<NSString *> *> *detailArray;
/**
 *  性别数组
 */
@property(nonatomic, strong) NSArray<NSString *> *sexArray;
/**
 *  当前界面的GUID
 */
@property(nonatomic, copy) NSString *guid;
/**
 *  空数组
 */
@property(nonatomic, strong) NSArray *nullArray;
/**
 *  第十八组的变化
 */
@property(nonatomic, assign) NSInteger section;
/**
 *  当前的偏移量
 1，刷新前后的偏移量要一直
 2，保存键盘弹出前tableview的contentOffset,方便我们在键盘收起时将tableview进行还原程原先的位置
 */
@property(nonatomic, assign) CGPoint lastContentOffset;
/**
 *  保存当前编辑cell的frame
 */
@property (nonatomic, assign) CGRect editFrame;
@end

@implementation ESTFollowUpRecordViewController
#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    self.guid = [TYZTool GUID];
    if (self.followUpType == FollowUpRecordType_Add) {
        self.firstFollowUp = YES;
        self.mainTitle = [@"新增客户" S];
        self.model = [[ESTCustomerInfoModel alloc] init];
        self.model.CUST_SEX = self.sexArray.firstObject;
        self.model.VE_ACTIVITY_ID = self.activity_id;
        self.model.SOURCE_BIG_CODE = [@"顾问" S];
        self.model.SOURCE_SMALL_CODE = [@"App录入" S];
        self.model.DLR_ID = UserInfo.EMP_INFO.firstObject.DLR_ID;
    } else {
        if (self.followUpType == FollowUpRecordType_Senbox) {
            self.mainTitle = [@"补充上次跟进" S];
            self.duration = self.callModel.dailTime;
            [self acquiredData];
        } else if (self.followUpType == FollowUpRecordType_Phone) {
            self.mainTitle = [@"通话跟进" S];
            self.custom_id = self.model.VE_ACTIVITY_CUST_ID;
            [self acquiredData];
        } else {
            self.mainTitle = [@"未通话跟进" S];
            self.custom_id = self.model.VE_ACTIVITY_CUST_ID;
            self.callModel = [TYZCallPhoneModel readModelInSendbox];
            if (self.callModel) {
                if ([self.callModel.activity_id isEqualToString:self.model.VE_ACTIVITY_ID]) {
                    WEAKSELF
                    if (![self.callModel.custom_id isEqualToString:self.model.VE_ACTIVITY_CUST_ID]) {
                        [TYZSystemDialog dialogWithTitle:[@"上次通话尚未跟进，是否继续上次跟进？如果点击取消，将会丢失上次的通话时长。" S] complement:^{
                            weakSelf.custom_id = weakSelf.callModel.custom_id;
                            [weakSelf acquiredData];
                        }];
                    } else {
                        [self acquiredData];
                    }
                } else {
                    [self acquiredData];
                }
            } else {
                [self acquiredData];
            }
        }
    }
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:[@"保存" S] style:UIBarButtonItemStylePlain target:self action:@selector(didClickRightButton)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    WEAKSELF
    // 跟进过程开始了
    [[NSNotificationCenter defaultCenter] addObserverForName:UITextViewTextDidBeginEditingNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification * _Nonnull note) {
        UITextView *textView = (UITextView *)note.object;
        if (textView.tag == TAG_SIXTH_ACTIVITY_FOLLOWUPTEXTVIEW) {
            weakSelf.editFrame = [textView convertRect:textView.bounds toView:nil];
        }
    }];
    // 跟进过程结束了
    [[NSNotificationCenter defaultCenter] addObserverForName:UITextViewTextDidEndEditingNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification * _Nonnull note) {
        UITextView *textView = (UITextView *)note.object;
        if (textView.tag == TAG_SIXTH_ACTIVITY_FOLLOWUPTEXTVIEW) {
            [weakSelf handleKeyboardHidden];
        }
    }];
    // 选择器隐藏了
    [[NSNotificationCenter defaultCenter] addObserverForName:Notification_PickerToolDidHidden object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification * _Nonnull note) {
        [weakSelf handleKeyboardHidden];
    }];
}
#pragma mark - actions
- (void)backToPreViewController {
    WEAKSELF
    if ([self.duration integerValue] > 0) {
        [TYZSystemDialog dialogWithTitle:[@"当前通话还未跟进，如果退出将会丢失本次的通话信息。退出请确认！" S] complement:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    } else {
        [TYZSystemDialog dialogWithTitle:[@"退出请确认" S] complement:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }
}
- (void)setEditFrame:(CGRect)editFrame {
    _editFrame = editFrame;
    [self handleKeyboardShowWithHeight:TYZPickerTool_Height];
}
- (void)handleKeyboardShowWithHeight:(CGFloat)height {
    CGRect frame = self.editFrame;
    //保存键盘弹出前tableview的contentOffset偏移
    self.lastContentOffset = self.tableView.contentOffset;
    //判断键盘弹出是否会遮挡当前编辑cell，frame.size.height是当前编辑cell的高度
    CGFloat offSet = frame.origin.y + frame.size.height - (kWindowHeight - height);
    //将试图的Y坐标向上移动offset个单位，以使线面腾出开的地方用于软键盘的显示
    if (offSet > 0.01) {
        WEAKSELF
        //有遮挡时，tableview需要的偏移量应该是在原先的基础上再往上上移的，这里我们默认增加10个单位的空白
        offSet += self.lastContentOffset.y + 10;
        [UIView animateWithDuration:0.1 animations:^{
            weakSelf.tableView.contentOffset = CGPointMake(0, offSet);
        }];
    }
}
- (void)handleKeyboardHidden {
    WEAKSELF
    [UIView animateWithDuration:0.1 animations:^{
        weakSelf.tableView.contentOffset = weakSelf.lastContentOffset;
    }];
}
- (void)acquiredData {
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTCustomerInfoModel enquiriesCustomerInfoWithID:self.custom_id success:^(NSArray<ESTCustomerInfoModel *> *modelArray, NSInteger dataNumber) {
        [TYZLoadingView hideHUD];
        if (modelArray.count > 0) {
            weakSelf.model = modelArray.firstObject;
            [weakSelf refreshTableViewAllData];
        } else {
            [TYZSystemDialog dialogWithTitle:[@"数据加载出错，可能会导致保存冲突，点击确定重试" S] complement:^{
                [weakSelf acquiredData];
            }];
        }
    } failure:^(NSString *error) {
        [TYZLoadingView hideHUD];
        [TYZSystemDialog dialogWithTitle:[@"数据加载出错，可能会导致保存冲突，点击确定重试" S] complement:^{
            [weakSelf acquiredData];
        }];
    }];
}
- (void)acquiredHistoryData {
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTFollowUpHistoryModel requestFollowUpHistoryDataWithCustomerID:self.custom_id success:^(NSArray<ESTFollowUpHistoryModel *> *modelArray) {
        [TYZLoadingView hideHUD];
        weakSelf.historyDataArray = modelArray;
    } failure:^(NSString *error) {
        [TYZLoadingView showError:error];
    }];
}
- (void)setHistoryDataArray:(NSArray<ESTFollowUpHistoryModel *> *)historyDataArray {
    _historyDataArray = historyDataArray;
    [self.cellHeightArray removeAllObjects];
    [historyDataArray enumerateObjectsUsingBlock:^(ESTFollowUpHistoryModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat height = FollowUpHistoryInfoCell_FirstPartHeight;
        NSArray *arr1 = [obj.FOLLOWUP_LABLE componentsSeparatedByString:@","];
        NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr1];
        [arr1 enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.length == 0) {
                [arrM removeObject:obj];
            }
        }];
        arr1 = [arrM mutableCopy];
        NSInteger lines = 0;
        if (arr1.count > 0) {
            lines = arr1.count / 4 + 1;
            if (arr1.count % 4 == 0) {
                lines--;
            }
        }
        if (lines == 0) {
            height += FollowUpHistoryInfoCell_LinePadding;
        } else {
            height += (FollowUpHistoryInfoCell_LinePadding + FollowUpHistoryInfoCell_LabelLineHeight) * lines;
        }
        
        NSInteger remarkLines = [obj.REMARK componentsSeparatedByString:@"\\r\\n"].count;
        if ([obj.REMARK hasSuffix:@"\\r\\n"]) {
            remarkLines--;
        }
        height += FollowUpHistoryInfoCell_RemarkLineHeight * remarkLines;
        height += FollowUpHistoryInfoCell_LinePadding;
        if (height < 90) {
            height = 90;
        }
        if (idx == 0) {
            height += 10;
        }
        [self.cellHeightArray addObject:@(height)];
    }];
    [self.tableView reloadData];
}
- (void)didClickRightButton {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if ([self.model.IS_INTENT integerValue] == 0 && [self.model.IS_NOLINK integerValue] == 0 && [self.model.IS_FAIL integerValue] == 0) {
        [TYZLoadingView showNormal:[@"请选择一个跟进状态" S]];
        return;
    }
    if (self.model.CUST_NAME.length < 1) {
        [TYZLoadingView showNormal:[@"请输入客户姓名" S]];
        return;
    }
    if (self.model.CUST_PHONE.length < 1) {
        [TYZLoadingView showNormal:[@"请输入电话" S]];
        return;
    }
    if (self.model.INTENT_CARTYPE_CN.length < 1) {
        [TYZLoadingView showNormal:[@"请选择意向车型" S]];
        return;
    }
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTCustomerInfoModel preserveCustomerInfoWithModel:self.model duration:self.duration guid:self.guid complement:^(NSString *message) {
        [TYZLoadingView hideHUD];
        if ([message isEqualToString:Request_Success]) {
            if (weakSelf.didClickPreserveBlock) {
                weakSelf.didClickPreserveBlock(YES);
            }
            [TYZLoadingView showSuccess:weakSelf.followUpType == FollowUpRecordType_Add ? [@"保存成功" S] : [@"跟进成功" S] complement:^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }];
        } else {
            [TYZLoadingView showError:message];
        }
    }];
}
#pragma mark - 设置UI
- (void)setupCurrendAndHistorySection:(UIView *)superView {
    if (self.followUpType == FollowUpRecordType_Add) return;
    [superView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    CGFloat width = kWindowWidth * 0.5;
    ESTActivityEffectiveView *childView1 = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:[@"本次录入" S]];
    [superView addSubview:childView1];
    [childView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(superView);
        make.left.equalTo(superView);
        make.width.mas_equalTo(width);
    }];
    ESTActivityEffectiveView *childView2 = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:[@"历史回访" S]];
    [superView addSubview:childView2];
    [childView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(superView);
        make.right.equalTo(superView);
        make.width.mas_equalTo(width);
    }];
    if (self.selectedHistory) {
        childView1.isSelected = NO;
        childView2.isSelected = YES;
    } else {
        childView1.isSelected = YES;
        childView2.isSelected = NO;
    }
    WEAKSELF
    __weak typeof(childView1) weakChildView1 = childView1;
    __weak typeof(childView2) weakChildView2 = childView2;
    [childView1 setDidCliclCoverBlock:^{
        weakChildView1.isSelected = YES;
        weakChildView2.isSelected = NO;
        weakSelf.selectedHistory = NO;
    }];
    [childView2 setDidCliclCoverBlock:^{
        weakChildView1.isSelected = NO;
        weakChildView2.isSelected = YES;
        weakSelf.selectedHistory = YES;
    }];
    
    TYZView *line = [[TYZView alloc] init];
    [superView addSubview:line];
    line.backgroundColor = TYZLightColor;
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(superView);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(2);
    }];
}
- (void)setSelectedHistory:(BOOL)selectedHistory {
    _selectedHistory = selectedHistory;
    if (selectedHistory) {
        [self.sectionNumber replaceObjectAtIndex:8 withObject:@"1"];
        self.headerViewLastDataArray = [self.headerViewDataArray mutableCopy];
        self.headerViewDataArray = [NSMutableArray arrayWithArray:self.headerViewHistoryDataArray];
        [self acquiredHistoryData];
    } else {
        [self.sectionNumber replaceObjectAtIndex:8 withObject:@"0"];
        self.headerViewDataArray = [NSMutableArray arrayWithArray:self.headerViewLastDataArray];
        [self.tableView reloadData];
    }
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.headerViewDataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0 || section == 6 || section == 8 || section == self.section) {
        return Value_Height_PartPadding;
    }
    if (section == 1 && self.followUpType == FollowUpRecordType_Add) {
        return 0;
    }
    if (section == 7) {
        if (self.firstFollowUp) {
            return 0;
        }
        return 40;
    }
    if (section == self.section + 1) {
        return 80;
    }
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0 || section == 6 || section == self.section) {
        TYZView *view = [[TYZView alloc] init];
        view.backgroundColor = ESTBackgroundColor;
        return view;
    }
    if (section == 7) {
        UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"ESTFollowUpRecordHeaderViewForHistory"];
        headerView.contentView.backgroundColor = TYZWhiteColor;
        [self setupCurrendAndHistorySection:headerView.contentView];
        return headerView;
    }
    if (section == 8) {
        TYZView *view = [[TYZView alloc] init];
        if (self.selectedHistory) {
            if (self.historyDataArray.count > 0) {
                view.backgroundColor = TYZWhiteColor;
                return view;
            }
        }
        view.backgroundColor = ESTBackgroundColor;
        return view;
    }
    ESTFollowUpRecordtHeaderView *headerView = [ESTFollowUpRecordtHeaderView creatFollowUpRecordtHeaderView:tableView];
    headerView.model = self.headerViewDataArray[section];
    WEAKSELF
    [headerView setDidSelectedOptionsBlock:^(NSInteger number, NSString *text) {
        ESTFollowUpRecordHeaderViewModel *model = weakSelf.headerViewDataArray[section];
        if ([model.leftTitle isEqualToString:weakSelf.titleArray[3].firstObject]) {// 客户性别
            weakSelf.model.CUST_SEX = text;
            model.selectedNumber = number;
            [weakSelf.headerViewDataArray replaceObjectAtIndex:section withObject:model];
            [weakSelf refreshTableViewDataWithSection:section];
        }
    }];
    [headerView setDidClickSwitchBlock:^(BOOL on) {
        [weakSelf handleSwitchState:on section:section];
    }];
    __weak typeof(headerView.contentView) weakContentView = headerView.contentView;
    [headerView setDidClickCoverBlock:^{
        ESTFollowUpRecordHeaderViewModel *model = weakSelf.headerViewDataArray[section];
        if ([model.leftTitle isEqualToString:weakSelf.titleArray[4].firstObject]) {// 选择意向车型
            ESTCarBrandTableViewController *controller = [[ESTCarBrandTableViewController alloc] init];
//            controller.enterModel = YES;
            controller.DLR_ID = weakSelf.model.DLR_ID;
            __weak typeof(model) weakModel = model;
            [controller setSelectedSeriesBlock:^(NSString *seriesName, NSString *seriesID) {
                weakSelf.model.INTENT_CARTYPE_CN = seriesName;
                weakSelf.model.INTENT_CARTYPE_ID = seriesID;
                weakModel.rightText = seriesName;
                [weakSelf.headerViewDataArray replaceObjectAtIndex:section withObject:weakModel];
                [weakSelf refreshTableViewDataWithSection:section];
            }];
            [weakSelf.navigationController pushViewController:controller animated:YES];
        } else if ([model.leftTitle isEqualToString:weakSelf.titleArray[14].firstObject]) {// 下次跟进时间
            weakSelf.editFrame = [weakContentView convertRect:weakContentView.frame toView:nil];
            __weak typeof(model) weakModel = model;
            [TYZPickerTool showDatePickerWithDateString:weakSelf.model.PLAN_FOLLOWUP_TIME datePickerMode:DatePickerMode_Date complement:^(NSString *dateString) {
                weakSelf.model.PLAN_FOLLOWUP_TIME = dateString;
                weakModel.rightText = dateString;
                [weakSelf.headerViewDataArray replaceObjectAtIndex:section withObject:weakModel];
                [weakSelf refreshTableViewDataWithSection:section];
            }];
        }
    }];
    [headerView setDidComplementTextFieldBlock:^(NSString *text) {
        ESTFollowUpRecordHeaderViewModel *model = weakSelf.headerViewDataArray[section];
        model.rightText = text;
        if ([model.leftTitle isEqualToString:weakSelf.titleArray[1].firstObject]) { // 客户姓名
            weakSelf.model.CUST_NAME = text;
        } else if ([model.leftTitle isEqualToString:weakSelf.titleArray[2].firstObject]) { // 客户电话
            weakSelf.model.CUST_PHONE = text;
        } else if ([model.leftTitle isEqualToString:weakSelf.titleArray[15].firstObject]) { // 跟进经过
            weakSelf.model.FOLLOWUP_CONTENT = text;
        }
        [weakSelf.headerViewDataArray replaceObjectAtIndex:section withObject:model];
        [weakSelf refreshTableViewDataWithSection:section];
    }];
    return headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.sectionNumber[section] integerValue] == 0) {
        return 0;
    }
    if (self.selectedHistory) {
        return self.historyDataArray.count;
    }
    return self.cellDataArray[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectedHistory) {
        if (self.historyDataArray.count > 0) {
            return self.cellHeightArray[indexPath.row].floatValue;
        }
        return kWindowHeight - (50 * 4 + 3 * Value_Height_PartPadding + 40) - DE_NavigationHeight;
    }
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectedHistory) {
        if (self.historyDataArray.count > 0) {
            if (indexPath.row == 0) {
                ESTFollowUpHistoryInfoCell *cell = [ESTFollowUpHistoryInfoCell creatFollowUpHistoryInfoCell:tableView modelNumber:self.historyDataArray.count];
                cell.model = self.historyDataArray[indexPath.row];
                return cell;
            }
            ESTFollowUpHistoryInfoCell *cell = [ESTFollowUpHistoryInfoCell creatFollowUpHistoryInfoCell:tableView];
            cell.model = self.historyDataArray[indexPath.row];
            return cell;
        }
        TYZTableViewCell *cell = [[TYZTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTFollowUpRecordViewControllerHistoryNullCell"];
        WEAKSELF
        [cell.contentView noDataWithClick:^{
            [weakSelf acquiredHistoryData];
        }];
        return cell;
    }
    ESTFollowUpRecordTableViewCell *cell = [ESTFollowUpRecordTableViewCell creatFollowUpRecordTableViewCell:tableView];
    cell.model = self.cellDataArray[indexPath.section][indexPath.row];
    WEAKSELF
    __weak typeof(cell.model) weakModel = cell.model;
    __weak typeof(indexPath) weakIndexPath = indexPath;
    __weak typeof(cell.contentView) weakContentView = cell.contentView;
    [cell setDidClickCoverBlock:^(NSString *title) {
        if ([title isEqualToString:weakSelf.detailArray[4].firstObject]) { // 报名到场日期
            weakSelf.editFrame = [weakContentView convertRect:weakContentView.frame toView:nil];
            [TYZPickerTool showDatePickerWithDateString:weakSelf.model.PLAN_COME_TIME datePickerMode:DatePickerMode_Date complement:^(NSString *dateString) {
                weakSelf.model.PLAN_COME_TIME = dateString;
                weakModel.rightText = dateString;
                [weakSelf.cellDataArray[weakIndexPath.section] replaceObjectAtIndex:2 withObject:weakModel];
                [weakSelf refreshTableViewDataWithRow:weakIndexPath.row inSection:weakIndexPath.section];
            }];
        } else if ([title isEqualToString:weakSelf.detailArray[5].firstObject]) { // 到场日期
            weakSelf.editFrame = [weakContentView convertRect:weakContentView.frame toView:nil];
            [TYZPickerTool showDatePickerWithDateString:weakSelf.model.REAL_COME_TIME datePickerMode:DatePickerMode_DateTime complement:^(NSString *dateString) {
                weakSelf.model.REAL_COME_TIME = [dateString substringToIndex:16];
                weakModel.rightText = [dateString substringToIndex:16];
                [weakSelf.cellDataArray[weakIndexPath.section] replaceObjectAtIndex:0 withObject:weakModel];
                [weakSelf refreshTableViewDataWithRow:weakIndexPath.row inSection:weakIndexPath.section];
            }];
        }
    }];
    [cell setDidComplementBlock:^(NSString *text) {
        [weakSelf handleTextFieldComplementWithText:text model:weakModel inIndexPath:weakIndexPath];
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
#pragma mark - setter & getter
- (NSArray<NSArray<NSString *> *> *)titleArray {
    if (_titleArray == nil) {
        _titleArray = @[
                        @[[@"本次通话时长" S], @""],
                        @[[@"客户姓名" S], [@"请输入客户姓名" S]],
                        @[[@"客户电话" S], [@"请输入客户电话" S]],
                        @[[@"客户性别" S], @""],
                        @[[@"意向车系" S], [@"请选择意向车系" S]],
                        @[[@"意向客户" S], @""],
                        @[[@"已加微信" S], @""],
                        @[[@"已交诚意金" S], @""],
                        @[[@"已下订金" S], @""],
                        @[[@"已报名到场" S], @""],
                        @[[@"待定到场" S], @""],
                        @[[@"实际到场" S], @""],
                        @[[@"未成功沟通" S], @""],
                        @[[@"是否战败" S], @""],
                        @[[@"下次跟进时间" S], @""],
                        @[[@"跟进进过" S], @"请输入跟进经过"]
                        ];
    }
    return _titleArray;
}
- (NSArray<NSArray<NSString *> *> *)detailArray {
    if (_detailArray == nil) {
        _detailArray = @[
                         @[[@"微信号" S], [@"请输入微信号" S]],
                         @[[@"诚意金" S], [@"选填，请输入诚意金金额" S]],
                         @[[@"到场客户姓名" S], [@"选填，请输入到场客户姓名" S]],
                         @[[@"到场人数" S], @""],
                         @[[@"报名到场日期" S], @""],
                         @[[@"到场日期" S], @""],
                         @[[@"战败描述" S], [@"请输入战败原因" S]],
                         ];
    }
    return _detailArray;
}
- (NSArray<NSString *> *)sexArray {
    if (_sexArray == nil) {
        _sexArray = @[
                      [@"男" S],
                      [@"女" S]
                      ];
    }
    return _sexArray;
}
- (NSArray *)nullArray {
    if (_nullArray == nil) {
        _nullArray = [NSArray array];
    }
    return _nullArray;
}
- (NSMutableArray<NSNumber *> *)cellHeightArray {
    if (_cellHeightArray == nil) {
        _cellHeightArray = [NSMutableArray array];
    }
    return _cellHeightArray;
}
- (void)setModel:(ESTCustomerInfoModel *)model {
    _model = model;
    if (self.callModel) {
        [TYZCallPhoneModel deleteModelInSendBox];
        self.callModel = nil;
    }
    [self handleOriginalData];
    [self initialAnalyseData];
}
#pragma mark - 根据值处理数据
- (void)initialAnalyseData {
    NSMutableArray<ESTFollowUpRecordHeaderViewModel *> *arrM1 = [NSMutableArray arrayWithArray:self.headerViewDataArray];
    NSMutableArray<NSArray<ESTFollowUpRecordCellModel *> *> *arrM2 = [NSMutableArray arrayWithArray:self.cellDataArray];
    if ([self.model.IS_INTENT integerValue] == 1) { // 意向客户  需要插入五项数据
        self.section = 19;
        [self.headerViewUnDataArray enumerateObjectsUsingBlock:^(ESTFollowUpRecordHeaderViewModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[ESTFollowUpRecordHeaderViewModel class]]) {
                if ([obj.leftTitle isEqualToString:self.titleArray[7].firstObject]) {// 已交诚意金
                    [arrM1 insertObject:obj atIndex:11];
                    [arrM2 insertObject:self.cellUnDataArray[idx] atIndex:11];
                    [self.sectionNumber insertObject:self.model.IS_PAY atIndex:11];
                } else if ([obj.leftTitle isEqualToString:self.titleArray[8].firstObject]) {// 已下订金
                    [arrM1 insertObject:obj atIndex:12];
                    [arrM2 insertObject:self.cellUnDataArray[idx] atIndex:12];
                    [self.sectionNumber insertObject:@"0" atIndex:11];
                } else if ([obj.leftTitle isEqualToString:self.titleArray[9].firstObject]) {// 已报名到场
                    [arrM1 insertObject:obj atIndex:13];
                    [arrM2 insertObject:self.cellUnDataArray[idx] atIndex:13];
                    [self.sectionNumber insertObject:self.model.IS_PLANCOME atIndex:11];
                } else if ([obj.leftTitle isEqualToString:self.titleArray[10].firstObject]) {// 待定到场
                    [arrM1 insertObject:obj atIndex:14];
                    [arrM2 insertObject:self.cellUnDataArray[idx] atIndex:14];
                    [self.sectionNumber insertObject:@"0" atIndex:11];
                } else if ([obj.leftTitle isEqualToString:self.titleArray[11].firstObject]) {// 实际到场
                    [arrM1 insertObject:obj atIndex:15];
                    [arrM2 insertObject:self.cellUnDataArray[idx] atIndex:15];
                    [self.sectionNumber insertObject:self.model.IS_COME atIndex:11];
                }
            }
        }];
    } else { // 非意向客户  需要移除五项数据
        self.section = 14;
        __block NSInteger index = 0;
        [self.headerViewDataArray enumerateObjectsUsingBlock:^(ESTFollowUpRecordHeaderViewModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[ESTFollowUpRecordHeaderViewModel class]]) {
                if ([obj.leftTitle isEqualToString:self.titleArray[7].firstObject]) {// 已交诚意金
                    [arrM1 removeObject:obj];
                    [arrM2 removeObjectAtIndex:idx - index];
                    [self recordRemoveHeaderDataWithIndex:idx];
                    [self recordRemoveCellDataWithIndex:idx];
                    [self.sectionNumber removeObjectAtIndex:idx - index];
                    index ++;
                } else if ([obj.leftTitle isEqualToString:self.titleArray[8].firstObject]) {// 已下订金
                    [arrM1 removeObject:obj];
                    [arrM2 removeObjectAtIndex:idx - index];
                    [self recordRemoveHeaderDataWithIndex:idx];
                    [self recordRemoveCellDataWithIndex:idx];
                    [self.sectionNumber removeObjectAtIndex:idx - index];
                    index ++;
                } else if ([obj.leftTitle isEqualToString:self.titleArray[9].firstObject]) {// 已报名到场
                    [arrM1 removeObject:obj];
                    [arrM2 removeObjectAtIndex:idx - index];
                    [self recordRemoveHeaderDataWithIndex:idx];
                    [self recordRemoveCellDataWithIndex:idx];
                    [self.sectionNumber removeObjectAtIndex:idx - index];
                    index ++;
                } else if ([obj.leftTitle isEqualToString:self.titleArray[10].firstObject]) {// 待定到场
                    [arrM1 removeObject:obj];
                    [arrM2 removeObjectAtIndex:idx - index];
                    [self recordRemoveHeaderDataWithIndex:idx];
                    [self recordRemoveCellDataWithIndex:idx];
                    [self.sectionNumber removeObjectAtIndex:idx - index];
                    index ++;
                } else if ([obj.leftTitle isEqualToString:self.titleArray[11].firstObject]) {// 实际到场
                    [arrM1 removeObject:obj];
                    [arrM2 removeObjectAtIndex:idx - index];
                    [self recordRemoveHeaderDataWithIndex:idx];
                    [self recordRemoveCellDataWithIndex:idx];
                    [self.sectionNumber removeObjectAtIndex:idx - index];
                    index ++;
                }
            }
        }];
    }
    [self.sectionNumber replaceObjectAtIndex:self.section - 2 withObject:self.model.IS_FAIL];
    self.headerViewDataArray = [arrM1 mutableCopy];
    self.cellDataArray = [arrM2 mutableCopy];
}
/**
 记录移除的HeaderView数据
 
 @param index 位置
 */
- (void)recordRemoveHeaderDataWithIndex:(NSInteger)index {
    ESTFollowUpRecordHeaderViewModel *model = self.headerViewDataArray[index];
    __block BOOL isContain = NO;
    __block NSInteger count = 0;
    [self.headerViewUnDataArray enumerateObjectsUsingBlock:^(ESTFollowUpRecordHeaderViewModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([model.leftTitle isEqualToString:obj.leftTitle]) {
            isContain = YES;
            count = idx;
            *stop = YES;
        }
    }];
    if (isContain) {
        [self.headerViewUnDataArray replaceObjectAtIndex:count withObject:model];
    } else {
        [self.headerViewUnDataArray addObject:self.headerViewDataArray[index]];
    }
}
/**
 记录移除清空的Cell数据
 
 @param index 位置
 */
- (void)recordRemoveCellDataWithIndex:(NSInteger)index {
    NSMutableArray<ESTFollowUpRecordCellModel *> *arr = self.cellDataArray[index];
    __block BOOL isContain = NO;
    __block NSInteger count = 0;
    [self.cellUnDataArray enumerateObjectsUsingBlock:^(NSMutableArray<ESTFollowUpRecordCellModel *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.firstObject.leftTitle isEqualToString:arr.firstObject.leftTitle]) {
            isContain = YES;
            count = idx;
            *stop = YES;
        }
    }];
    if (isContain) {
        [self.cellUnDataArray replaceObjectAtIndex:count withObject:arr];
    } else {
        [self.cellUnDataArray addObject:arr];
    }
}
/**
 处理最原始的数据
 */
- (void)handleOriginalData {
    NSMutableArray *sectionArrM = [NSMutableArray arrayWithCapacity:21];
    NSMutableArray *numberArrM = [NSMutableArray arrayWithCapacity:21];
    NSMutableArray *historyArrM = [NSMutableArray arrayWithCapacity:8];
    [self.titleArray enumerateObjectsUsingBlock:^(NSArray<NSString *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ESTFollowUpRecordHeaderViewModel *headerViewModel = [[ESTFollowUpRecordHeaderViewModel alloc] init];
        headerViewModel.leftTitle = obj.firstObject;
        headerViewModel.placeholder = obj.lastObject;
        headerViewModel.textFieldTag = TAG_SIXTH_FOLLOWUPRECORD_SECTIONTags + idx;
        NSString *number = @"0";
        switch (idx) {
            case 0:
                headerViewModel.rightText = [NSString stringWithFormat:@"%ld''", [self.duration integerValue]];
                headerViewModel.isNotEnable = YES;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_TextField;
                break;
            case 1:
                headerViewModel.maxWords = 12;
                headerViewModel.rightText = self.model.CUST_NAME;
                headerViewModel.returnKeyType = UIReturnKeyNext;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_TextField;
                break;
            case 2:
                headerViewModel.maxWords = 15;
                headerViewModel.rightText = self.model.CUST_PHONE;
                headerViewModel.keyboardType = UIKeyboardTypeNumberPad;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_TextField;
                break;
            case 3: {
                NSInteger count = [self.sexArray indexOfObject:self.model.CUST_SEX];
                if (count != 1) self.model.CUST_SEX = self.sexArray.firstObject;
                headerViewModel.optionArray = self.sexArray;
                headerViewModel.selectedNumber = count == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Options;
            }
                break;
            case 4:
                headerViewModel.hiddenLine = YES;
                headerViewModel.rightText = self.model.INTENT_CARTYPE_CN;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_SelectedBlock;
                break;
            case 5:
                headerViewModel.switch_state = [self.model.IS_INTENT integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 6:
                number = self.model.IS_ADDWECHAT;
                headerViewModel.switch_state = [number integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 7:
                number = self.model.IS_PAY;
                headerViewModel.switch_state = [number integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 8:
                headerViewModel.switch_state = [self.model.IS_ORDER integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 9:
                number = self.model.IS_PLANCOME;
                headerViewModel.switch_state = [number integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 10:
                headerViewModel.switch_state = [self.model.IS_READYCOME integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 11:
                number = self.model.IS_COME;
                headerViewModel.switch_state = [number integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 12:
                headerViewModel.switch_state = [self.model.IS_NOLINK integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 13:
                number = self.model.IS_FAIL;
                headerViewModel.switch_state = [number integerValue] == 1;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_Switch;
                break;
            case 14:
                self.model.PLAN_FOLLOWUP_TIME = [self.model.PLAN_FOLLOWUP_TIME getLocalDateFormate];
                if (!self.model.PLAN_FOLLOWUP_TIME || self.model.PLAN_FOLLOWUP_TIME.length < 10) {
                    self.model.PLAN_FOLLOWUP_TIME = [TYZTool getAfterSomeDay:1 format:@"yyyy-MM-dd"];
                } else if (self.model.PLAN_FOLLOWUP_TIME.length > 10) {
                    self.model.PLAN_FOLLOWUP_TIME = [self.model.PLAN_FOLLOWUP_TIME substringToIndex:10];
                }
                headerViewModel.hiddenLine = YES;
                headerViewModel.rightText = self.model.PLAN_FOLLOWUP_TIME;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_SelectedBlock;
                break;
            case 15:
                headerViewModel.maxWords = 200;
                headerViewModel.hiddenLine = YES;
                headerViewModel.headerViewType = FollowUpTableViewHeaderViewType_TextView;
                break;
            default:
                break;
        }
        if (idx < 5) {
            [historyArrM addObject:headerViewModel];
        }
        [sectionArrM addObject:headerViewModel];
        [numberArrM addObject:number];
    }];
    [sectionArrM insertObject:@"" atIndex:0];
    [numberArrM insertObject:@"0" atIndex:0];
    [historyArrM insertObject:@"" atIndex:0];
    [sectionArrM insertObject:@"" atIndex:6];
    [numberArrM insertObject:@"0" atIndex:6];
    [historyArrM insertObject:@"" atIndex:6];
    [sectionArrM insertObject:@"" atIndex:7];
    [numberArrM insertObject:@"0" atIndex:7];
    [historyArrM insertObject:@"" atIndex:7];
    [sectionArrM insertObject:@"" atIndex:8];
    [numberArrM insertObject:@"0" atIndex:8];
    [historyArrM insertObject:@"" atIndex:8];
    [sectionArrM insertObject:@"" atIndex:19];
    [numberArrM insertObject:@"0" atIndex:19];
    self.headerViewDataArray = [sectionArrM mutableCopy];
    self.sectionNumber = [numberArrM mutableCopy];
    self.headerViewUnDataArray = [NSMutableArray array];
    self.headerViewHistoryDataArray = [historyArrM mutableCopy];
    
    NSMutableArray *rowArrM = [NSMutableArray arrayWithCapacity:14];
    NSMutableArray *arrM1 = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *arrM2 = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *arrM3 = [NSMutableArray arrayWithCapacity:3];
    NSMutableArray *arrM4 = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *arrM5 = [NSMutableArray arrayWithCapacity:1];
    [self.detailArray enumerateObjectsUsingBlock:^(NSArray<NSString *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ESTFollowUpRecordCellModel *cellModel = [[ESTFollowUpRecordCellModel alloc] init];
        cellModel.leftTitle = obj.firstObject;
        cellModel.placeholder = obj.lastObject;
        cellModel.textFieldTag = TAG_SIXTH_FOLLOWUPRECORD_ROWTags + idx;
        cellModel.maxWords = 15;
        switch (idx) {
            case 0:
                cellModel.rightText = self.model.WECHAT;
                cellModel.cellType = FollowUpTableViewCellType_TextField;
                [arrM1 addObject:cellModel];
                break;
            case 1:
                cellModel.rightText = self.model.PAY_MONEY;
                cellModel.keyboardType = UIKeyboardTypeNumberPad;
                cellModel.cellType = FollowUpTableViewCellType_TextField;
                [arrM2 addObject:cellModel];
                break;
            case 2:
                cellModel.isNotEnable = YES;
                cellModel.rightText = self.model.CUST_NAME;
                cellModel.cellType = FollowUpTableViewCellType_TextField;
                [arrM3 addObject:cellModel];
                break;
            case 3:
                cellModel.maxValue = 99;
                if ([self.model.PLAN_COME_NUMBER integerValue] < 1) {
                    self.model.PLAN_COME_NUMBER = @"1";
                }
                cellModel.rightText = self.model.PLAN_COME_NUMBER;
                cellModel.cellType = FollowUpTableViewCellType_Number;
                [arrM3 addObject:cellModel];
                break;
            case 4:
                self.model.PLAN_COME_TIME = [self.model.PLAN_COME_TIME getLocalDateFormate];
                if (!self.model.PLAN_COME_TIME || self.model.PLAN_COME_TIME.length < 10) {
                    self.model.PLAN_COME_TIME = [TYZTool getTomorrowDay:[NSDate date]];
                } else if (self.model.PLAN_COME_TIME.length > 10) {
                    self.model.PLAN_COME_TIME = [self.model.PLAN_COME_TIME substringToIndex:10];
                }
                cellModel.rightText = self.model.PLAN_COME_TIME;
                cellModel.cellType = FollowUpTableViewCellType_Picker;
                [arrM3 addObject:cellModel];
                break;
            case 5:
                self.model.REAL_COME_TIME = [self.model.REAL_COME_TIME getLocalDateFormate];
                if (!self.model.REAL_COME_TIME || self.model.REAL_COME_TIME.length < 16) {
                    self.model.REAL_COME_TIME = [[NSDate date] formatterDate:@"yyyy-MM-dd HH:mm"];
                } else if (self.model.PLAN_COME_TIME.length > 16) {
                    self.model.REAL_COME_TIME = [self.model.REAL_COME_TIME substringToIndex:16];
                }
                cellModel.rightText = self.model.REAL_COME_TIME;
                cellModel.cellType = FollowUpTableViewCellType_Picker;
                [arrM4 addObject:cellModel];
                break;
            case 6:
                cellModel.maxWords = 50;
                cellModel.rightText = self.model.FAIL_DESC;
                cellModel.cellType = FollowUpTableViewCellType_TextField;
                [arrM5 addObject:cellModel];
                break;
            default:
                break;
        }
    }];
    [rowArrM addObject:arrM1];
    [rowArrM addObject:arrM2];
    [rowArrM addObject:arrM3];
    [rowArrM addObject:arrM4];
    [rowArrM addObject:arrM5];
    [rowArrM insertObject:self.nullArray atIndex:0];
    [rowArrM insertObject:self.nullArray atIndex:1];
    [rowArrM insertObject:self.nullArray atIndex:2];
    [rowArrM insertObject:self.nullArray atIndex:3];
    [rowArrM insertObject:self.nullArray atIndex:4];
    [rowArrM insertObject:self.nullArray atIndex:5];
    [rowArrM insertObject:self.nullArray atIndex:6];
    [rowArrM insertObject:self.nullArray atIndex:7];
    [rowArrM insertObject:self.nullArray atIndex:8];
    [rowArrM insertObject:self.nullArray atIndex:9];
    [rowArrM insertObject:self.nullArray atIndex:12];
    [rowArrM insertObject:self.nullArray atIndex:14];
    [rowArrM insertObject:self.nullArray atIndex:16];
    [rowArrM insertObject:self.nullArray atIndex:18];
    [rowArrM insertObject:self.nullArray atIndex:19];
    [rowArrM insertObject:self.nullArray atIndex:20];
    self.cellDataArray = [rowArrM mutableCopy];
    self.cellUnDataArray = [NSMutableArray array];
}
- (void)handleSwitchState:(BOOL)on section:(NSInteger)section {
    ESTFollowUpRecordHeaderViewModel *model = self.headerViewDataArray[section];
    model.switch_state = on;
    [self.headerViewDataArray replaceObjectAtIndex:section withObject:model];
    if ([model.leftTitle isEqualToString:self.titleArray[5].firstObject]) {// 意向客户
        self.model.IS_INTENT = [NSString stringWithFormat:@"%d", on];
        [self initialAnalyseData];
        if (on) {
            // 不是战败，也不是未沟通
            if ([self.model.IS_FAIL integerValue] == 1) {
                self.model.IS_FAIL = @"0";
                ESTFollowUpRecordHeaderViewModel *model2 = self.headerViewDataArray[self.section - 2];
                model2.switch_state = NO;
                [self.headerViewDataArray replaceObjectAtIndex:self.section - 2 withObject:model2];
            }
            if ([self.model.IS_NOLINK integerValue] == 1) {
                self.model.IS_NOLINK = @"0";
                ESTFollowUpRecordHeaderViewModel *model3 = self.headerViewDataArray[self.section - 3];
                model3.switch_state = NO;
                [self.headerViewDataArray replaceObjectAtIndex:section - 3 withObject:model3];
            }
        }
    } else {
        NSString *value = [self.sectionNumber[section] integerValue] ? @"0" : @"1";
        [self.sectionNumber replaceObjectAtIndex:section withObject:value];
        if ([model.leftTitle isEqualToString:self.titleArray[9].firstObject]) {// 报名到场
            self.model.IS_PLANCOME = [NSString stringWithFormat:@"%d", on];
            if (on) {
                if ([self.model.IS_READYCOME integerValue] == 1) {
                    self.model.IS_READYCOME = @"0";
                    [self.sectionNumber replaceObjectAtIndex:section + 1 withObject:@"0"];
                    ESTFollowUpRecordHeaderViewModel *model2 = self.headerViewDataArray[section + 1];
                    model2.switch_state = NO;
                    [self.headerViewDataArray replaceObjectAtIndex:section + 1 withObject:model2];
                }
            }
        } else if ([model.leftTitle isEqualToString:self.titleArray[10].firstObject]) {// 待定到场
            self.model.IS_READYCOME = [NSString stringWithFormat:@"%d", on];
            if (on) {
                if ([self.model.IS_PLANCOME integerValue] == 1) {
                    self.model.IS_PLANCOME = @"0";
                    [self.sectionNumber replaceObjectAtIndex:section - 1 withObject:@"0"];
                    ESTFollowUpRecordHeaderViewModel *model2 = self.headerViewDataArray[section - 1];
                    model2.switch_state = NO;
                    [self.headerViewDataArray replaceObjectAtIndex:section - 1 withObject:model2];
                }
            }
        } else if ([model.leftTitle isEqualToString:self.titleArray[12].firstObject]) {// 未成功沟通
            self.model.IS_NOLINK = [NSString stringWithFormat:@"%d", on];
            if (self.firstFollowUp) {
                if (on) {
                    // 就不是意向客户，也不是战败
                    if ([self.model.IS_INTENT integerValue] == 1) {
                        self.model.IS_INTENT = @"0";
                        ESTFollowUpRecordHeaderViewModel *model2 = self.headerViewDataArray[9];
                        model2.switch_state = NO;
                        [self.headerViewDataArray replaceObjectAtIndex:9 withObject:model2];
                        [self initialAnalyseData];
                    }
                    if ([self.model.IS_FAIL integerValue] == 1) {
                        self.model.IS_FAIL = @"0";
                        ESTFollowUpRecordHeaderViewModel *model3 = self.headerViewDataArray[self.section - 2];
                        model3.switch_state = NO;
                        [self.headerViewDataArray replaceObjectAtIndex:self.section - 2 withObject:model3];
                        [self.sectionNumber replaceObjectAtIndex:self.section - 2 withObject:@"0"];
                    }
                }
            }
        } else if ([model.leftTitle isEqualToString:self.titleArray[13].firstObject]) {// 战败
            self.model.IS_FAIL = [NSString stringWithFormat:@"%d", on];
            if (on) {
                // 不是意向客户，也不是未沟通
                if ([self.model.IS_INTENT integerValue] == 1) {
                    self.model.IS_INTENT = @"0";
                    ESTFollowUpRecordHeaderViewModel *model2 = self.headerViewDataArray[9];
                    model2.switch_state = NO;
                    [self.headerViewDataArray replaceObjectAtIndex:9 withObject:model2];
                    [self initialAnalyseData];
                }
                if ([self.model.IS_NOLINK integerValue] == 1) {
                    self.model.IS_NOLINK = @"0";
                    ESTFollowUpRecordHeaderViewModel *model3 = self.headerViewDataArray[self.section - 3];
                    model3.switch_state = NO;
                    [self.headerViewDataArray replaceObjectAtIndex:self.section - 3 withObject:model3];
                }
            }
        } else if ([model.leftTitle isEqualToString:self.titleArray[6].firstObject]) {// 已加微信
            self.model.IS_ADDWECHAT = [NSString stringWithFormat:@"%d", on];
        } else if ([model.leftTitle isEqualToString:self.titleArray[7].firstObject]) {// 已交诚意金
            self.model.IS_PAY = [NSString stringWithFormat:@"%d", on];
        } else if ([model.leftTitle isEqualToString:self.titleArray[8].firstObject]) {// 已下订金
            self.model.IS_ORDER = [NSString stringWithFormat:@"%d", on];
        } else if ([model.leftTitle isEqualToString:self.titleArray[9].firstObject]) {// 实际到场
            self.model.IS_COME = [NSString stringWithFormat:@"%d", on];
        }
    }
    [self refreshTableViewAllData];
}
- (void)handleTextFieldComplementWithText:(NSString *)text model:(ESTFollowUpRecordCellModel *)model inIndexPath:(NSIndexPath *)indexPath {
    NSInteger index = 0;
    if ([model.leftTitle isEqualToString:self.detailArray[0].firstObject]) { // 微信号
        self.model.WECHAT = text;
    } else if ([model.leftTitle isEqualToString:self.detailArray[1].firstObject]) { // 诚意金
        self.model.PAY_MONEY = text;
    } else if ([model.leftTitle isEqualToString:self.detailArray[2].firstObject]) { // 到场客户姓名
        
    } else if ([model.leftTitle isEqualToString:self.detailArray[3].firstObject]) { // 到场人数
        index = 1;
    } else if ([model.leftTitle isEqualToString:self.detailArray[6].firstObject]) { // 战败描述
        self.model.FAIL_DESC = text;
    }
    model.rightText = text;
    [self.cellDataArray[indexPath.section] replaceObjectAtIndex:index withObject:model];
    [self refreshTableViewDataWithRow:indexPath.row inSection:indexPath.section];
}
- (void)refreshTableViewAllData {
    self.lastContentOffset = self.tableView.contentOffset;
    [self.tableView reloadData];
    self.tableView.contentOffset = self.lastContentOffset;
}
- (void)refreshTableViewDataWithSection:(NSInteger)section {
    self.lastContentOffset = self.tableView.contentOffset;
//    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView reloadData];
    self.tableView.contentOffset = self.lastContentOffset;
}
- (void)refreshTableViewDataWithRow:(NSInteger)row inSection:(NSInteger)section {
    self.lastContentOffset = self.tableView.contentOffset;
//    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:section]] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView reloadData];
    self.tableView.contentOffset = self.lastContentOffset;
}
- (BOOL)fd_interactivePopDisabled {
    return YES;
}
@end
